//
//  FootRefreshView.m
//  MPTLive
//
//  Created by caixun on 16/1/29.
//  Copyright © 2016年 Jeakin. All rights reserved.
//

#import "FootRefreshView.h"


@interface FootRefreshView ()

@property (nonatomic, strong) UILabel *textLabel;
@property (nonatomic, strong) UIActivityIndicatorView *indicatorView;

@end


@implementation FootRefreshView

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        [self initInterface];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self initInterface];
    }
    return self;
}

- (void)initInterface
{
    self.backgroundColor = [UIColor clearColor];
    
    if (!self.indicatorView)
    {
        self.indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        self.indicatorView.hidesWhenStopped = YES;
        self.indicatorView.color = [UIColor yellowColor];
        [self addSubview:self.indicatorView];
    }
    
    if (!self.textLabel)
    {
        self.textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        self.textLabel.backgroundColor = [UIColor clearColor];
        self.textLabel.textColor = [UIColor blackColor];
        self.textLabel.textAlignment = NSTextAlignmentCenter;
        self.textLabel.font = [UIFont systemFontOfSize:12.0f];
        [self addSubview:self.textLabel];
    }
    
    self.indicatorView.frame = CGRectMake(20,
                                          (self.frame.size.height-self.indicatorView.frame.size.height)/2,
                                          self.indicatorView.frame.size.width,
                                          self.indicatorView.frame.size.height);
    
    [self YXRefreshViewDidChangedState:YXRefreshViewStateStopped];
}

- (void)YXRefreshViewDidChangedState:(YXRefreshViewState)state
{
    switch (state)
    {
        case YXRefreshViewStateStopped:
            self.textLabel.text = @"上拉加载更多";
            break;
        case YXRefreshViewStateTriggered:
            self.textLabel.text = @"上拉加载更多";
            break;
        case YXRefreshViewStateNoData:
            self.textLabel.text = @"没有更多可加载了...";
            break;
            
        default:
            self.textLabel.text = @"玩命加载中...";
            break;
    }
    
    if (state == YXRefreshViewStateLoading)
    {
        [self.indicatorView startAnimating];
        self.textLabel.hidden = NO;
    }
    else
    {
        [self.indicatorView stopAnimating];
        self.textLabel.hidden = NO;
    }
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
}

+ (FootRefreshView *)createFootRefresh
{
    FootRefreshView *view = [[FootRefreshView alloc] initWithFrame:CGRectMake(0, 0, screenW, 40.0f)];
    
    return view;
}

@end
